<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Focus-related events should fire in the correct order</title>
  <link rel="help" href="https://w3c.github.io/uievents/#events-focusevent-event-order">
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script src="/resources/testdriver.js"></script>
  <script src="/resources/testdriver-actions.js"></script>
  <script src="/resources/testdriver-vendor.js"></script>
  <script src="/uievents/resources/eventrecorder.js"></script>
</head>

<body>
  <ol>
    <li>Click into the text input.</li>
    <li>Click just below the text input.</li>
    <li>Click into the text input again.</li>
    <li>Click the "Done" button.</li>
  </ol>
  <div id="a" tabindex="1" style="width:400px; height:300px">
    <br /><br /><br />
    <input type="text", id="b"></div>
  </div>
  <button type="button" id="done">Done</button>
</body>

<script>
setup({explicit_timeout: true});
function stopPropagation(e) {
  e.stopPropagation();
}
var relevantEvents = [
  "focus",
  "blur",
  "focusin",
  "focusout"
];
window.onload = async function () {
  var a = document.getElementById("a");
  var b = document.getElementById("b");
  var button = document.getElementById("done");
  var inputs = [a, b];
  EventRecorder.configure({
      objectMap: {
         "a": a,
         "b": b,
      }
    });

  EventRecorder.addEventListenersForNodes(relevantEvents, inputs, stopPropagation);
  var expected = [
    {type: "focus", target: "b"},
    {type: "focusin", target: "b"},
    {type: "blur", target: "b"},
    {type: "focusout", target: "b"},
    {type: "focus", target: "a"},
    {type: "focusin", target: "a"},
    {type: "blur", target: "a"},
    {type: "focusout", target: "a"},
    {type: "focus", target: "b"},
    {type: "focusin", target: "b"},
    {type: "blur", target: "b"},
    {type: "focusout", target: "b"}
  ];

  async_test(function(t) {
    button.addEventListener("click", function () {
      t.step(function () {
        assert_true(EventRecorder.checkRecords(expected));
        t.done();
      });
    }, false);
  }, "Focus-related events should fire in the correct order");
  EventRecorder.start();

  await new test_driver.Actions()
    .pointerMove(0, 0, {origin: b})
    .pointerDown()
    .pointerUp()
    .pointerMove(0, 0, {origin: a})
    .pointerDown()
    .pointerUp()
    .pointerMove(0, 0, {origin: b})
    .pointerDown()
    .pointerUp()
    .pointerMove(0, 0, {origin: button})
    .pointerDown()
    .pointerUp()
    .send();
};
</script>
</html>